#include "controlW.h"
Control::Control(QWidget *parent) : 
QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // First Line: TCP Server IP and Port
    QHBoxLayout *tcpLayout = new QHBoxLayout();
    QLineEdit *ipLineEdit = new QLineEdit(this);
    ipLineEdit->setPlaceholderText("IP Address");
    QRegularExpression ipRegex(
        "^(\\d{1,3}\\.){3}\\d{1,3}$");
    QRegularExpressionValidator *ipValidator = new QRegularExpressionValidator(ipRegex, this);
    ipLineEdit->setValidator(ipValidator);
    ipLineEdit->setText("127.0.0.1");
    QLineEdit *portLineEdit = new QLineEdit(this);
    portLineEdit->setPlaceholderText("Port");
    portLineEdit->setText("14560");
    portLineEdit->setValidator(new QIntValidator(0, 65535, this));
    QPushButton *connectButton = new QPushButton("链接", this);
    tcpLayout->addWidget(new QLabel("IP:"));
    tcpLayout->addWidget(ipLineEdit);
    tcpLayout->addWidget(new QLabel("Port:"));
    tcpLayout->addWidget(portLineEdit);
    tcpLayout->addWidget(connectButton);

    // Second Line: Flight Altitude
    QHBoxLayout *altitudeLayout = new QHBoxLayout();
    QLineEdit *altitudeLineEdit = new QLineEdit(this);
    altitudeLineEdit->setPlaceholderText("Altitude");
    altitudeLineEdit->setText("10");
    altitudeLineEdit->setValidator(new QIntValidator(0, 10000, this)); // Check for reasonable altitude range.
    QPushButton *takeoffButton = new QPushButton("一键起飞", this);
    altitudeLayout->addWidget(new QLabel("Altitude:"));
    altitudeLayout->addWidget(altitudeLineEdit);
    altitudeLayout->addWidget(takeoffButton);

    // Third Line: Start, Emergency Stop, Land
    QHBoxLayout *controlButtonLayout = new QHBoxLayout();
    QPushButton *startButton = new QPushButton("开始", this);
    QPushButton *emergencyStopButton = new QPushButton("急停", this);
    QPushButton *landButton = new QPushButton("降落", this);
    controlButtonLayout->addWidget(startButton);
    controlButtonLayout->addWidget(emergencyStopButton);
    controlButtonLayout->addWidget(landButton);

    mainLayout->addLayout(tcpLayout);
    mainLayout->addLayout(altitudeLayout);
    mainLayout->addLayout(controlButtonLayout);

    connect(connectButton, &QPushButton::clicked, this, [=]() {
        emit connectClicked(ipLineEdit->text().toStdString(), portLineEdit->text().toInt());
    });

    connect(takeoffButton, &QPushButton::clicked, this, [=]() {
        int cmd_type  = (int)CmdType::TAKEOFF;
        int drone_id = 0;
        std::vector<float> data_f;
        data_f.push_back(altitudeLineEdit->text().toDouble());
        std::vector<int> data_i={};
        std::string data_s="";
        emit cmdBtnClicked(cmd_type,drone_id,data_f,data_i,data_s);
    });

    connect(startButton, &QPushButton::clicked, this, [=]() {
        int cmd_type  = (int)CmdType::WAYPOINT;
        int drone_id = 0;
        std::vector<float> data_f={};
        std::vector<int> data_i={};
        std::string data_s="";
        emit cmdBtnClicked(cmd_type,drone_id,data_f,data_i,data_s);
    });

    connect(emergencyStopButton, &QPushButton::clicked, this,[=]() {
        int cmd_type  = (int)CmdType::STOP;
        int drone_id = 0;
        std::vector<float> data_f={};
        std::vector<int> data_i={};
        std::string data_s="";
        emit cmdBtnClicked(cmd_type,drone_id,data_f,data_i,data_s);
    });

    connect(landButton, &QPushButton::clicked, this, [=]() {
        int cmd_type  = (int)CmdType::LAND;
        int drone_id = 0;
        std::vector<float> data_f = {};
        std::vector<int> data_i={};
        std::string data_s="";
        emit cmdBtnClicked(cmd_type,drone_id,data_f,data_i,data_s);
    });
}